package com.diipo.weibo.adapter;

import java.io.UnsupportedEncodingException;
import java.util.LinkedList;

import com.diipo.weibo.AsyncImageLoader;
import com.diipo.weibo.MsgActivity;
import com.diipo.weibo.R;
import com.diipo.weibo.entity.Msg;
import com.diipo.weibo.utils.TimeUtil;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MsgItemAdapter extends BaseAdapter{
	
	private LinkedList<Msg> mList = new LinkedList<Msg>();
	private Context mctx;
	private AsyncImageLoader asyncImageLoader = AsyncImageLoader.getInstance();
	private String TAG = MsgItemAdapter.class.getSimpleName();

	public MsgItemAdapter(LinkedList<Msg> mList, Context mctx) {
		super();
		this.mList = mList;
		this.mctx = mctx;
	}


	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mList == null ? 0 : mList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}


	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		MsgItem item = null;
		if (convertView == null) {
			item = new MsgItem();
			LayoutInflater inflater = LayoutInflater.from(mctx);
			convertView = View.inflate(mctx, R.layout.msg_item, null);
			item.icon = (ImageView) convertView.findViewById(R.id.msg_item_icon);
			item.name = (TextView) convertView.findViewById(R.id.msg_item_name);
			item.createTime = (TextView) convertView.findViewById(R.id.msg_item_createTime);
			item.content = (TextView) convertView.findViewById(R.id.msg_item_content);
			convertView.setTag(item);
		}
		item = (MsgItem) convertView.getTag();
		final Msg msg = mList.get(position);
		//Log.e(TAG," position = "+ position + ",id = "+msg.getId()+" ,url = " + msg.getFrom().getProfile_picture());
		asyncImageLoader.loadPortrait(msg.getFrom().getProfile_picture(), item.icon);
		item.name.setText(msg.getFrom().getUsername());
		item.createTime.setText(TimeUtil.getDate(msg.getCreated_time()));
		item.content.setText(msg.getText());
//		Log.e(" pyb ", " MsgItemAdapter getView() called ,position = " + position);
		return convertView;
	}

}
